<?php
require_once("../DAO/UserDAO.php");
include_once("../fckeditor/fckeditor.php") ;
include_once("../DAO/CategoryDAO.php");
include_once("../DAO/QuestionDAO.php");
include_once("../Entities/Question.php");
include_once("../Utility/WebUtilities.php");
include_once("../Utility/WebSession.php");
include_once("../Utility/FunctionUtilities.php");

if(WebSession::CurrentUserNoReturn() == null || WebSession::CurrentUserNoReturn()->Role != WebUtilities::GetAdminRoleId())
{
	WebUtilities::Redirect("index.php");
}

if(isset($_POST["FCKeditor1"]) && $_POST["FCKeditor1"] != "")
{
	$questionDAO = new QuestionDAO();
	if(!isset($_GET["id"]))
	{
		$item = new Question();
		$item->Content = $_POST["FCKeditor1"];
		$item->Category = $_POST["cbCategories"];
		$item->Paragraph = $_REQUEST["paragraphId"];
		$item->Sound = FunctionUtilities::SaveUploadSound($_FILES['uploadfile'], true);
		$item = $questionDAO->CreateQuestion($item);
	}
	else
	{
		$item = $questionDAO->GetQuestionById($_GET["id"]);
		if($item != null)
		{
			$item->Content = $_POST["FCKeditor1"];
			$item->Category = $_POST["cbCategories"];
			$oldPath = $item->Sound;
			$item->Sound = FunctionUtilities::SaveUploadSound($_FILES['uploadfile'], true);
			if($item->Sound != "")
				FunctionUtilities::DeleteFile($oldPath);
			else
				$item->Sound = $oldPath;
			$questionDAO->UpdateQuestion($item);
		}
	}
	WebUtilities::Redirect("QuestionManagement.php" . ($_POST["paragraphId"] != 0 ? ("?paragraphId=" . $_POST["paragraphId"]) : ""));
}

$oFCKeditor = new FCKeditor('FCKeditor1') ;
$oFCKeditor->BasePath = '../fckeditor/' ;
$oFCKeditor->Value = '' ;
$oFCKeditor->Width = '78%';
$oFCKeditor->Height = '400';

$selectValue = -1;
$answerManagement = "";
$hdQuestionId = "<input type='hidden' id='hdQuestionId' value='0' />";
$paragraphId = WebUtilities::GetQueryString("paragraphId", 0);
$submitUrl = "EditQuestion.php?paragraphId=" . $paragraphId;
if(isset($_GET["id"]))
{
	$questionDAO = new QuestionDAO();
	$item = $questionDAO->GetQuestionById($_GET["id"]);
	if($item != null)
	{
		$selectValue = $item->Category;
		$oFCKeditor->Value = $item->Content;
		$answerManagement = "<a class='right' href='#' onclick='ShowPopup(" . $item->Id ."); return false;'>Go to Answer Management Page</a>";
		$submitUrl = "EditQuestion.php?paragraphId=" . $paragraphId . "&id=" . $item->Id;
		$hdQuestionId = "<input type='hidden' id='hdQuestionId' value='" . $item->Id ."' />";
	}
	else
	{
		$answerManagement = "<a class='right' href='#' onclick='return false;'>After you create this question , you can add the answers</a>";
	}
}
else
{
	$answerManagement = "<a class='right' href='#' onclick='return false;'>After you create this question , you can add the answers</a>";
}

$categoryDAO = new CategoryDAO();
$optionStr = "";
$categoryList = $categoryDAO->GetAll();
for($i = 0; $i < count($categoryList); $i++)
{
	if($selectValue == $categoryList[$i]->Id)
		$selectedIndex = "selected='true'";
	else
		$selectedIndex = "";
	$optionStr = $optionStr . "<option value='" . $categoryList[$i]->Id . "' " . $selectedIndex . ">" . $categoryList[$i]->Name . "</option>";
} 

$mainContent = "<form enctype='multipart/form-data' action='" . $submitUrl ."' method='POST'>
				<div class='oneLine'>
					<div class='left'>Categories</div>
					<select class='right' name='cbCategories'>" .
						$optionStr
					 . "</select>
				</div>
				<div class='oneLine'>
					<div class='left'>Sound</div>
					<input class='right' type='file' name='uploadfile'/>
				</div>
				<div class='oneLine'>
					<div class='left'>Answers</div>" .
					$answerManagement .	
				"</div>
				<div class='oneLine'>
					<div class='left'>Content</div>";

$mainContent = $mainContent . $oFCKeditor->CreateHtml() . "</div>
				<div class='oneLine'>
					<div class='left'>&nbsp;</div>
					<div>
						<input type='submit' value='Save' name='btSave'/>
						<input type='button' value='Cancel' onclick=\"window.location='QuestionManagement.php?paragraphId=" . $paragraphId . "'\" />
					</div>
				</div>
				<input name='paragraphId' type='hidden' value='" . $paragraphId . "' />
				</form>";
				
$mainContent = $mainContent . "<div id='popupPanel' class='popupPanel'>"
									. $hdQuestionId .
									"<a href='#' onclick='ClosePopupPanel(); return false;'>Close</a>
									<div id='answerPanel'>
										<h1>Answer Management</h1>
										<table class='file' id='tableAnswers'>
											<thead>
												<tr>
													<th><input type='checkbox' id='checkAllItem' name='checkAllItem' onclick='checkProcess.checkAll(this.checked)' /></th>
													<th>Answer Id</th>
													<th>Content</th>
													<th>Correct</th>
													<th>Action</th>
												</tr>
											</thead>
											<tbody class='tableBody'> 
												
											</tbody>
										</table>
										<br/>
										<div>
											<input type='button' value='Delete' />
											<input type='button' value='Add' onclick='ShowAnswerPanel(0)' />
										</div>
									</div>
									<div id='editAnswerPanel'>
										<h2 id='answerHeader'>Edit Answer</h2>
										<div class='oneLine'>
											<div class='left'>Type</div>
											<input type='radio' id='rbCorrect' name='rbCorrect'>Correct</input>
											<input type='radio' id='rbIncorrect' name='rbCorrect' checked>Incorrect</input>
										</div>
										<div class='oneLine'>
											<div class='left'>Content</div>
											<input type='text' id='tbContent' style='width:350px;height:180px;' />
										</div>
										<div class='oneLine'>
											<div class='left'>&nbsp;</div>
											<div>
												<input type='submit' value='Save' name='btSave' onclick='SaveAnswer()'/>
												<input type='button' value='Cancel' onclick='HideAnswerPanel()' />
											</div>
										</div>
										<input type='hidden' id='hdAnswerId' value='0' />
									</div>
								</div>";
$pageIndex = 5;
include("MasterPage.php");
?>